//
//  UINavigationBar+More.m
//  CarUser
//
//  Created by ayi on 2019/1/10.
//  Copyright © 2019 王森林. All rights reserved.
//

#import "UINavigationBar+More.h"

@implementation UINavigationBar (More)

- (void)setCustomBgColor:(UIColor *)color{
    
    UIView *oneview = [self valueForKey:@"backgroundView"];
    if (oneview.subviews.count > 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, IS_IPHONE_X ? - 44 : -20, self.bounds.size.width, NAV_HEIGHT)];
        view.backgroundColor = color;
        [self setValue:view forKey:@"backgroundView"];
    }else{
        oneview.backgroundColor = color;
    }

//    [self setBackgroundImage:[CommonMethod createImageWithColor:color WithFrame:CGRectMake(0, 0, BOUNDWIDTH, IS_IPHONE_X?88:64)] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
    
}

- (void)setCustomBackgroundAlpha:(CGFloat)alpha{
    // 导航栏背景透明度设置
    UIView *barBackgroundView = [self.subviews objectAtIndex:0];// _UIBarBackground
    UIView *backgroundImageView = [[barBackgroundView subviews] objectAtIndex:0];// UIImageView
    if (self.isTranslucent) {
        if (backgroundImageView != nil) {
            if ([backgroundImageView isKindOfClass:[UIImageView class]]) {
                if (((UIImageView *)backgroundImageView).image != nil) {
                    barBackgroundView.alpha = alpha;
                }
            }
            barBackgroundView.alpha = alpha;
        }else{
            UIView *backgroundEffectView = [[barBackgroundView subviews] objectAtIndex:1];// UIVisualEffectView
            if (backgroundEffectView != nil) {
                backgroundEffectView.alpha = alpha;
            }
        }
    } else {
        barBackgroundView.alpha = alpha;
    }
    self.clipsToBounds = alpha == 0.0;
}


@end
